'use strict'

angular.module('mainApp')
.controller('userCtrl',function($scope){
})
.controller('orderCtrl',function($scope, tjzHttp, constant, ngDialog, $state){
    var dateChange = function(list){
        for(var i=0;i<list.length;i++){
            // if(list[i].date){
            //     list[i].date = list[i].date.split('.0')[0];
            // }
            if(list[i].updateDate){
                list[i].updateDate = list[i].updateDate.split('.0')[0];
            }
        }
        return list;
    }
    var init = function(){
        tjzHttp.postRequest(constant.url + '/getOrder',{}).then(function(res){
            if(res.data.code == 0){
                dateChange(res.data.list);
                $scope.orders = res.data.list;
            }
        })
    }
    $scope.deleteOrder = function(id){
        tjzHttp.postRequest(constant.url + '/delete/order',{id:id}).then(function(res){
            if (res.data.code == 0) {
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '订单操作成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function(){
                    $state.reload()
                });
            } else {
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        var message = $scope.ngDialogData.message;
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
    init();
})
.controller('favoriteCtrl',function($scope, tjzHttp, constant, $state, ngDialog){
    var imageUrlChange = function(list){
        for(var i=0;i<list.length;i++){
            if(list[i].imageUrl){
                var a = list[i].imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                list[i].imageUrl = a;
            }
        }
        return list;
    }
    var init = function(){
        tjzHttp.postRequest(constant.url + '/favorite/job/get').then(function(res){
            if(res.data.code == 0){
                imageUrlChange(res.data.list);
                $scope.favoriteJob = res.data.list;
            }
        })
        tjzHttp.postRequest(constant.url + '/favorite/user/get').then(function(res){
            if(res.data.code == 0){
                $scope.favoriteUser = res.data.list;
            }
        })
    }
    $scope.deleteFavJob = function(id){
        tjzHttp.postRequest(constant.url + '/favorite/job/delete', {id:id}).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '删除收藏成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function () {
                    $state.reload();
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope','ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                })
            }
        })
        event.stopPropagation();
    }
    $scope.deleteFavUser = function(id){
        tjzHttp.postRequest(constant.url + '/favorite/user/delete', {id:id}).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '删除收藏成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function () {
                    $state.reload();
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope','ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                })
            }
        })
        event.stopPropagation();
    }
    init();
})
.controller('mycreateCtrl',function($scope, tjzHttp, constant, $state, ngDialog){
    var init = function(){
        tjzHttp.postRequest(constant.url + '/mycreate/get').then(function(res){
            if(res.data.code == 0){
                $scope.mycreateList = res.data.list.reverse();
                for(var i=0;i<$scope.mycreateList.length;i++){
                    $scope.mycreateList[i].date = $scope.mycreateList[i].date.split('.0')[0];
                }
            }
        })
    }
    $scope.deleteJob = function(id, jobId){
        ngDialog.open({
            template: 'template/dialog.html',
            width: 600,
            data: {
                id: id,
                jobId: jobId
            },
            controller: ['$scope', 'ngDialog','tjzHttp', 'constant', '$state', function ($scope, ngDialog, tjzHttp, constant, $state) {
                var id = $scope.ngDialogData.id;
                var jobId = $scope.ngDialogData.jobId;
                $scope.dialogTitle = '淘家政提示';
                $scope.dialogContent = '确认下架当前发布吗？';
                $scope.yes = function () {
                    var data = {
                        id: id,
                        jobId: jobId
                    }
                    tjzHttp.postRequest(constant.url + '/mycreate/delete', data).then(function(res){
                        if(res.data.code == 0){
                            $scope.closeThisDialog();
                            ngDialog.open({
                                template: 'template/dialog.html',
                                width: 600,
                                controller: ['$scope', function ($scope) {
                                    $scope.dialogTitle = '淘家政提示';
                                    $scope.dialogContent = '下架成功';
                                    $scope.yes = function () {
                                        $scope.closeThisDialog();
                                    }
                                }]
                            }).closePromise.then(function () {
                                $state.reload();
                            });
                        }else{
                            var message = res.data.message;
                            $scope.closeThisDialog();
                            ngDialog.open({
                                template: 'template/dialog.html',
                                width: 600,
                                data: {
                                    message: message
                                },
                                controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                                    $scope.dialogTitle = '淘家政提示';
                                    $scope.dialogContent = $scope.ngDialogData.message;
                                    $scope.yes = function () {
                                        $scope.closeThisDialog();
                                    }
                                }]
                            })
                        }
                    })
                }
            }]
        }).closePromise.then(function () {
            $scope.hasAuthenticate = false;
        });
        tjzHttp.postRequest(constant)
    }
    init();
})
.controller('historyCtrl',function($scope, constant, tjzHttp, $state, ngDialog){
    var imageUrlChange = function(list){
        for(var i=0;i<list.length;i++){
            if(list[i].imageUrl){
                var a = list[i].imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                list[i].imageUrl = a;
            }
        }
        return list;
    }
    var init = function(){
        tjzHttp.postRequest(constant.url + '/get/history').then(function(res){
            if(res.data.code == 0){
                imageUrlChange(res.data.list);
                $scope.historyList = res.data.list;
            }
        })
    }
    $scope.deleteHistory = function(){
        tjzHttp.postRequest(constant.url + '/delete/history').then(function(res){
            if (res.data.code == 0) {
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '清空历史记录成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function(){
                    $state.reload()
                });
            } else {
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        var message = $scope.ngDialogData.message;
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
    init();
})
.controller('addressCtrl',function($scope){})
.controller('authenticateCtrl',function($scope, tjzHttp, ngDialog, constant, $state){
    $scope.sexType = [{
        value: 1,
        sex: '男'
    },{
        value: 0,
        sex: '女'
    }];
    var init = function(){
        tjzHttp.postRequest(constant.url + '/isCreateInfo').then(function(res){
            if(res.data.code == 0){
                if(res.data.data){
                    $scope.hasAuthenticate = true;
                }else{
                    $scope.hasAuthenticate = false;
                }

            }
        })
    }
    init();
    $scope.deleteAuthenticate = function(){
        tjzHttp.postRequest(constant.url + '/deleteUserInfo').then(function (res) {
            if (res.data.code == 0) {
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '取消实名认证成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function () {
                    $scope.hasAuthenticate = false;
                });
            }
        })
    }
    $scope.save = function() {
        var url = constant.url + '/createUserInfo';
        var data = {
                realName: $scope.realName,
                sex: $scope.sex,
                age: $scope.age,
                idnumber: $scope.idnumber
            };
        tjzHttp.postRequest(url, data).then(function (res) {
            if (res.data.code == 0) {
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '实名认证成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function(){
                    $state.reload()
                });
            } else {
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        var message = $scope.ngDialogData.message;
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
})